var _ = require('lodash');
var moment = require('moment');
var nj = require('nunjucks');
var join = require('path').join;
var dataProxy = require('./util/getCDNData');

var VIEWS_PATH = join(__dirname, '../htdocs/views');

module.exports = function*(next) {
	var req = this.request;
	var res = this.response;

	//处理tms页面
	if (/taskList\.html?$/.test(req.pathname)) {
		var taskList =
			yield taskDao.query({});
		var html = nj.render(join(VIEWS_PATH, 'taskList.nj'), {
			'list': taskList,
			'tmsType': cons.TMS_TYPE_TASK
		});

		res.status(200)
			.type('html')
			.data(html);
			
		return;
	}

	// API接口调试页面
	/*
	if (/getAPI\.html?$/.test(req.pathname)) {
		var html = nj.render(join(VIEWS_PATH, 'getAPI.nj'), {
			version: '1.0.0'
		})
		
		res.status(200)
			.type('html')
			.data(html);
		
		return;
	}
	*/

	// 首页
	if (/index\.html?$/.test(req.pathname)) {
		var html = nj.render(join(VIEWS_PATH, 'index.nj'), {
			back: false,
			head_title: 'cdn助手-首页',
			title: 'cdn助手'
		});

		res.status(200)
			.type('html')
			.data(html);

		return;
	}

	// 查询CDN服务状态
	if (/describeCdnService\.html?$/.test(req.pathname)) {
		var resp =
			yield dataProxy.getCDNData.bind(dataProxy, 'describeCdnService', {});

		// 数据处理
		var dataHanlder = function(resp) {
			if (!resp) return;

			// 计费类型
			if (resp.InternetChargeType) {
				resp.InternetChargeType = (resp.InternetChargeType == 'PayByTraffic' ? '流量计费' : '宽带计费');
			}

			// 下次生效的计费类型
			if (resp.ChangingChargeType) {
				resp.ChangingChargeType = (resp.ChangingChargeType == 'PayByTraffic' ? '流量计费' : '宽带计费');
			}

			// 开通服务时间
			if (resp.OpeningTime) {
				resp.OpeningTime = moment(resp.OpeningTime).format('YYYY:MM:DD hh:MM:ss');
			}

			// 下次生效时间
			if (resp.ChangingAffectTime) {
				resp.ChangingAffectTime = moment(resp.ChangingAffectTime).format('YYYY:MM:DD hh:MM:dd');
			}

			if (resp.OperationLocks.LockReason.length > 0) {
				resp.status = '欠费';
			} else {
				resp.status = '正常';
			}

			return resp;
		};

		// 执行数据处理函数
		dataHanlder(resp);

		var html = nj.render(join(VIEWS_PATH, 'describeCdnService.nj'), {
			back: true,
			head_title: 'cdn助手-查询CDN服务状态',
			title: 'CDN服务状态',
			data: resp
		});

		res.status(200)
			.type('html')
			.data(html);
			
		return;
	}

	// 查询域名状态
	if (/describeUserDomains\.html?$/.test(req.pathname)) {
		var resp =
			yield dataProxy.getCDNData.bind(dataProxy, 'describeUserDomains', {
				PageSize: 10,
				PageNumber: 1
			});

		// 数据处理
		var dataHanlder = function(resp) {
			if (!resp) return;

			for (var i = 0, len = resp.Domains.PageData.length; i < len; i++) {
				var item = resp.Domains.PageData[i];

				switch (item.CdnType) {
					case 'web':
						item.CdnType = '图片小文件';
						break;
					case 'download':
						item.CdnType = '大文件下载';
						break;
					case 'video':
						item.CdnType = '视频流媒体';
						break;
					default:
						item.CdnType = '图片小文件';
						break;
				}
			}

			return resp;
		};

		// 执行数据处理函数
		dataHanlder(resp);

		var html = nj.render(join(VIEWS_PATH, 'describeUserDomains.nj'), {
			back: true,
			head_title: 'cdn助手-查询域名状态',
			title: '域名状态',
			data: resp
		});

		res.status(200)
			.type('html')
			.data(html);
			
		return;
	}

	// 添加加速域名
	if (/addCdnDomain\.html?$/.test(req.pathname)) {
		var html = nj.render(join(VIEWS_PATH, 'addCdnDomain.nj'), {
			back: true,
			head_title: 'cdn助手-添加加速域名',
			title: '添加加速域名',
			//data: resp
			data: {}
		});

		res.status(200)
			.type('html')
			.data(html);
			
		return;
	}
	
	// 预热文件
	if (/pushObjectCache\.html?$/.test(req.pathname)) {
		
		var html = nj.render(join(VIEWS_PATH, 'pushObjectCache.nj'), {
			back: true,
			head_title: 'cdn助手-预热文件',
			title: '预热文件',
			data: []
		});
				
		
		res.status(200)
			.type('html')
			.data(html);
			
		return;
	}
	
	// 监控数据
	if (/chart\.html?$/.test(req.pathname)) {
		
		var html = nj.render(join(VIEWS_PATH, 'chart.nj'), {
			back: true,
			head_title: 'cdn助手-监控数据',
			title: '监控数据',
			data: []
		});
		
		res.status(200)
			.type('html')
			.data(html);
		
		return;
	}

	// 刷新文件内容
	if (/refreshObjectCaches\.html?$/.test(req.pathname)) {
		var html = nj.render(join(VIEWS_PATH, 'refreshObjectCaches.nj'), {
			back: true,
			head_title: 'cdn助手-刷新文件内容',
			title: '刷新文件内容',
			//data: resp
			data: {}
		});

		res.status(200)
			.type('html')
			.data(html);
			
		return;
	}
	
	// 查询预热状态
	if (/describeRefreshTasks\.html?$/.test(req.pathname)) {
		var html = nj.render(join(VIEWS_PATH, 'describeRefreshTasks.nj'), {
			back: true,
			head_title: 'cdn助手-查询预热状态',
			title: '查询预热状态',
			//data: resp
			data: {}
		});

		res.status(200)
			.type('html')
			.data(html);
			
		return;
	}

	yield next;
}